package jtk.project4.fleet.screen.customerManagement;

/*
* Author : Rio Dermawan 
* Date Created : 1-April-2011
* Date Modified : 6-April-2011
* Version : 1.1
* */


import java.awt.FlowLayout;
import java.util.ArrayList;

import jtk.project4.fleet.action.AddCustomerAction;
import jtk.project4.fleet.action.CloseCustomerAction;
import jtk.project4.fleet.action.CustomerManagementAction;
import jtk.project4.fleet.action.DeleteCustomerAction;
import jtk.project4.fleet.action.EditCustomerAction;
import jtk.project4.fleet.domain.Customers;
import jtk.project4.fleet.domain.Vendor;
import jtk.project4.fleet.event.CustomerManagementEvent;
import jtk.project4.fleet.event.VendorEvent;
import jtk.project4.fleet.event.CustomerManagementEvent.CustomerManagementEventType;
import jtk.project4.fleet.event.VendorEvent.VendorEventType;
import jtk.project4.fleet.field.CustomerManagementField;


import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.TableField;
import nl.coderight.jazz.form.field.TableField.AutoResizeMode;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.i18n.Message;
import nl.coderight.jazz.i18n.Message.Choice;
import nl.coderight.jazz.i18n.Message.Param;
import nl.coderight.jazz.view.StatusBar;
	
public class CustomerManagementView extends FormView {
	private PushButton closeButton;
	private PushButton addButton;
	private TableField<Customers> customerTable;
	private PushButton editButton;
	private PushButton deleteButton;

	private Message status;
	
	public void handleEvent (CustomerManagementEvent evt){
		Customers selectedCustomers = customerTable.getValue();
		switch(evt.getType()){
		case CLOSE:
			close();
		case CHANGE:
			//bwt filter
		case SELECT :
						
			ArrayList<Choice> choices = new ArrayList<Choice>();
			choices.add(new Choice(0, "status.message.none"));
			choices.add(new Choice(1, "status.message.one"));
			choices.add(new Choice(2, "status.message.all"));
			
			status = new Message("status.message");
			status.addParam(new Param(customerTable.getValueCount()));
			status.addParam(new Param(customerTable.getListOfValues().size(), choices));
			status.addParam(new Param(customerTable.getSelectionCount()));
			// display message on status bar

		break;
		case DELETE:
			Message message = new Message("dialog.delete.message");
			message.addParam(new Param(selectedCustomers.getName()));
			message.addParam(new Param(selectedCustomers.getCountry()));
			
			OptionDialog dialog = new OptionDialog(this);
			dialog.setTitle("dialog.delete.title");
			dialog.setOptionType(OptionType.YES_NO);
			dialog.setOptions("Yes", "No", "button.cancel");
			dialog.setDefaultOption("button.no");
			dialog.setMessageType(MessageType.QUESTION);
			dialog.setMessage(message);
			dialog.show();			
			if (dialog.getReturnOptionType() == ReturnOptionType.YES) {
				propagateEvent(evt);
			}
			break;
		case FILTER:
			// filterField is changed, update filter
			break;
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		

		}
	}
	@Override
	protected void init() {
		createFields();
		createLayout();	
		
	}

	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(closeButton);
		buttons.addField(addButton);
		buttons.addField(editButton);
		buttons.addField(deleteButton);
		return buttons;
	}
	
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		form.addField(customerTable, NORTH);
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private void createLayout() {
		addField(createForm());
	}
	
	private void createFields(){
		customerTable = new TableField<Customers> ("customers", "customer");
		customerTable.setTitle("customers.header.table");
		customerTable.setAutoResizeMode(AutoResizeMode.OFF);
		customerTable.getSelectionValues();

		
		//setup column
		//customerTable.addColumn("VendorID","vendorId");
		customerTable.addColumn("Name","name");
		customerTable.addColumn("Contact","contact");
		customerTable.addColumn("Address1","address1");
		customerTable.addColumn("Address2","address2");
		customerTable.addColumn("City","city");
		customerTable.addColumn("State_","state");
		customerTable.addColumn("Zip","zip");
		customerTable.addColumn("Country","country");
		customerTable.addColumn("Phone1","phone1");
		customerTable.addColumn("Phone2","phone2");
		customerTable.addColumn("Fax","fax");
		customerTable.addColumn("Email","email");
		customerTable.addColumn("Comments","comments");
		customerTable.addColumn("Type_","type");
		customerTable.addColumn("Terms","terms");
		customerTable.addColumn("Website","website");
		
		//customerTable.setOnSelectEvent(new CustomerManagementEvent(CustomerManagementEventType.SELECT));
		//customerTable.setOnChangeEvent(new CustomerManagementEvent(CustomerManagementEventType.CHANGE));
		//customerTable.setOnEditEvent(new CustomerManagementEvent(CustomerManagementEventType.EDIT));
		
		
		closeButton = new PushButton(new CloseCustomerAction());
		addButton = new PushButton(new AddCustomerAction());
		editButton = new PushButton(new EditCustomerAction());
		deleteButton = new PushButton(new DeleteCustomerAction());
		
	}
}
